package com.example.application.views.study;

import com.example.application.bean.ClassBean;
import com.example.application.bean.StudentBean;
import com.example.application.views.main.MainView;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.model.*;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.PropertyId;
import com.vaadin.flow.data.converter.StringToIntegerConverter;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import java.util.ArrayList;
import java.util.List;

@Route(value = "student",layout = MainView.class)
@PageTitle("学生信息展示")
public class StudentView extends Div {
    //创建绑定器
    private Binder<StudentBean> binder = new Binder<>(StudentBean.class);

    @PropertyId("name")
    private  TextField nameTextField;

    private   RadioButtonGroup<String> gender;

    private TextField age;

    private ComboBox<ClassBean> classBean;

    public  StudentView(){

        FormLayout layout = new FormLayout();
         //姓名
        nameTextField = new TextField();
        nameTextField.setLabel("学生姓名");
        nameTextField.setPlaceholder("请输入学生姓名");
        //性别
        gender= new RadioButtonGroup<>();
        gender.setLabel("性别");
        gender.setItems("男", "女");
        gender.setValue("男");
        //年龄
        age = new TextField();
        age.setLabel("年龄");
        age.setPlaceholder("请输入年龄");
        //所在班级
        List<ClassBean> classBeanList = new ArrayList<>();
        classBeanList.add(new ClassBean(1,"班级1"));
        classBeanList.add(new ClassBean(2,"班级2"));
        classBeanList.add(new ClassBean(3,"班级3"));
        classBeanList.add(new ClassBean(4,"班级4"));

        classBean = new ComboBox<>();
        classBean.setLabel("所在班级");
        classBean.setItems(classBeanList);
        classBean.setValue(classBeanList.get(0));
        classBean.setItemLabelGenerator((classBean1)->{
           return  classBean1.getName();
        });

        Button button = new Button("绑定数据");
       //绑定点击事件
        button.addClickListener((e)->{
           //获得绑定对象的值
           // StudentBean studentBean = binder.getBean();

            //System.out.println(studentBean);
            StudentBean studentBean = new StudentBean(1,"绑定的值","男",20,new ClassBean(3,"班级3"));

            binder.setBean(studentBean);


        });

        layout.add(nameTextField, gender, age,classBean,button);

        layout.setResponsiveSteps(
                new FormLayout.ResponsiveStep("1px", 1),
                new FormLayout.ResponsiveStep("600px", 2),
                new FormLayout.ResponsiveStep("700px", 3));

        //把对象绑定到对应的表单中（手动绑定）
        // nameField 表单控件
        // name 表示对应对象的属性的值
       // binder.forField(nameField).bind("name");
       // binder.forField(genderGroup).bind("gender");
        //binder.forField(ageField).bind("age");
       // binder.forField(ageField).withConverter(new StringToIntegerConverter("请输入整数")).bind("age");
        //binder.forField(comboBox).bind("classBean");
        //先邦一个空的对象
        binder.setBean(new StudentBean());
        binder.forField(age).withConverter(new StringToIntegerConverter("请输入整数")).bind("age");
        //自动绑定
        binder.bindInstanceFields(this);

        //----------表格-----------
        Chart chart = new Chart(ChartType.PIE);

        Configuration conf = chart.getConfiguration();

        conf.setTitle("Browser market shares in January, 2018");

        Tooltip tooltip = new Tooltip();
        tooltip.setValueDecimals(1);
        conf.setTooltip(tooltip);

        PlotOptionsPie plotOptions = new PlotOptionsPie();
        plotOptions.setAllowPointSelect(true);
        plotOptions.setCursor(Cursor.POINTER);
        plotOptions.setShowInLegend(true);
        conf.setPlotOptions(plotOptions);

        DataSeries series = new DataSeries();
        DataSeriesItem chrome = new DataSeriesItem("Chrome", 61.41);
        chrome.setSliced(true);
        chrome.setSelected(true);
        series.add(chrome);
        series.add(new DataSeriesItem("Internet Explorer", 11.84));
        series.add(new DataSeriesItem("Firefox", 10.85));
        series.add(new DataSeriesItem("Edge", 4.67));
        series.add(new DataSeriesItem("Safari", 4.18));
        series.add(new DataSeriesItem("Sogou Explorer", 1.64));
        series.add(new DataSeriesItem("Opera", 6.2));
        series.add(new DataSeriesItem("QQ", 1.2));
        series.add(new DataSeriesItem("Others", 2.61));
        conf.setSeries(series);
        chart.setVisibilityTogglingDisabled(true);

        add(layout,chart);


    }
}
